#!/bin/bash

# 设置退出脚本当命令失败时 (非零退出状态)
#set -e

# 函数：运行单元测试
run_unit_tests() {
    local pattern="unit_test_*"
    
    # 遍历所有匹配模式的目录
    for dir in $pattern; do
        if [ -d "$dir" ] && [ -x "$dir/$(basename "$dir")" ]; then
            echo "Running tests in $dir and Collecting coverage data..."
            # 收集代码覆盖率数据
	    (cd "$dir" && "./$(basename "$dir")" && (find ./ -name '*.o' | xargs gcov --preserve-paths))
        else
	    echo "Skipping non-existent or non-executable directory: $dir, $dir/$(basename "$dir")"
        fi
    done
}

rm moc_*
#收集覆盖率报告
find ./ -name "*.o" | xargs gcov

# 上传 result.zip 到平台
upload_result() {
/home/panjinrui/.sonar/sonar-scanner-6.0.0.4432-linux/bin/sonar-scanner \
-Dsonar.projectKey=desktop_kylin-screenshot \
-Dsonar.sources=./unit_test_default \
-Dsonar.host.url=https://codescan.kylinos.cn \
-Dsonar.cfamily.gcov.reportsPath=./unit_test_default \
-Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
-Dsonar.branch.name=yhkylin/v11-2503

/home/panjinrui/.sonar/sonar-scanner-6.0.0.4432-linux/bin/sonar-scanner \
-Dsonar.projectKey=desktop_kylin-screenshot \
-Dsonar.sources=./kylin_screenshot/src \
-Dsonar.host.url=https://codescan.kylinos.cn \
-Dsonar.cfamily.gcov.reportsPath=./tests/unit_test_default \
-Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
-Dsonar.branch.name=yhkylin/v11-2503

  sonar-scanner \
    -Dsonar.projectKey=desktop_kylin-screenshot \
    -Dsonar.sources=./kylin_screenshot/src  \
    -Dsonar.host.url=https://codescan.kylinos.cn \
    -Dsonar.coverageReportPaths=./test/.report/sonarqube-coverage.xml \
    -Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
    -Dsonar.branch.name=yhkylin/v11-2503
#\
#-Dsonar.coverage.exclusions="**"
}

# 编译项目
echo "Compiling the project..."
#qmake && make

# 运行各个单元测试
echo "Running unit tests..."
#run_unit_tests

# 上传到平台
upload_result

echo "All tests have been run successfully, and coverage data has been collected."
